
package gs.eisenhower.swallet.dao;

import gs.eisenhower.swallet.db.WalletSqlHelper;
import gs.eisenhower.swallet.model.Wallet;
import gs.eisenhower.swallet.utils.constants.WalletConstants;
import gs.eisenhower.swallet.utils.methods.QueryBuilder;

import java.util.ArrayList;
import java.util.List;

import android.database.Cursor;

public class WalletDaoImpl extends GenericDaoImpl implements WalletDao {
	
	public WalletDaoImpl() {
	}

	@Override
	public List<Double> readPerCategory(String categoryName) {
		QueryBuilder queryBuilder = new QueryBuilder();
		final String WHERE_STATMENT = WalletSqlHelper.CATEGORY_COLUMN + "='"
				+ categoryName + WalletConstants.APOSTROPHE;
		
		queryBuilder.SELECT(WalletSqlHelper.AMOUNT_COLUMN);
		queryBuilder.FROM(WalletSqlHelper.WALLET_TABLE_NAME);
		queryBuilder.WHERE(WHERE_STATMENT);		
		Cursor cursor = open().rawQuery(queryBuilder.toString(), null);		
		List<Double> amount = new ArrayList<Double>();
		if (cursor.moveToFirst()) {
			amount.add(cursor.getDouble(0));
			while (cursor.moveToNext()) {
				amount.add(cursor.getDouble(0));
			}
		}
		cursor.close();
		close();

		if (amount.size() == 0) {
			amount.add(0.0);
		}
		return amount;
	}

	@Override
	public List<Wallet> readAll() {
		QueryBuilder queryBuilder = new QueryBuilder();
		queryBuilder.SELECT(WalletConstants.ASTERISK);
		queryBuilder.FROM(WalletSqlHelper.WALLET_TABLE_NAME);
		queryBuilder.ORDER_BY_DESC();
		
		List<Wallet> transactions = new ArrayList<Wallet>();
		Cursor cursor = open().rawQuery(queryBuilder.toString(), null);
		if (cursor.moveToFirst()) {
			do {
				Wallet wallet = new Wallet();
				wallet.setAmount(cursor.getDouble(1));
				wallet.setCategory(cursor.getString(2));
				wallet.setDetails(cursor.getString(3));
				wallet.setType(cursor.getString(4));
				transactions.add(wallet);
			} while (cursor.moveToNext());
		}
		cursor.close();
		close();
		
		return transactions;
	}
}
